package com.znfeiche.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLUtils {
	public static final String XML_PATH = "config";
	public static final String xml_name = "list.xml";
	public static void main(String[] args) {
//		readMenu("config/list.xml");
		readMenu(XML_PATH+File.separator+"list.xml");
		updateMenu(XML_PATH+File.separator+"list.xml");
		delMenu(XML_PATH+File.separator+"list.xml");
	}
	/**
	 * 读取内容
	 * @param fileName
	 */
	public static void readMenu(String fileName){
		SAXReader reader = new SAXReader();
		Document doc = null;
		try {
			doc = reader.read(new File(fileName));
			Element root = doc.getRootElement();
			List<Element> eles   = root.elements("menu");
			for(Element ele :eles){
				String name = ele.getTextTrim();
				String id = ele.attributeValue("id");
				int level = Integer.parseInt(ele.attributeValue("level"));
				
				System.out.println("name:"+name+"  id:"+id+"  level:"+level);
				
			}
			
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 修改菜单
	 * @param fileName
	 */
	public static void updateMenu(String fileName){
		SAXReader reader = new SAXReader();
		Document doc = null;
		try {
			doc = reader.read(new File(fileName));
			Element root = doc.getRootElement();
			List<Element> eles   = root.elements("menu");
			Element menu  = root.addElement("menu");
			menu.addText("菜单二");
			menu.addAttribute("id", "02");
			menu.addAttribute("level", "2");
			 //指定文件输出的位置
	        FileOutputStream out =new FileOutputStream(fileName);
	        
	        // 指定文本的写出的格式：
	        OutputFormat format=OutputFormat.createPrettyPrint();   //漂亮格式：有空格换行
	        format.setEncoding("UTF-8");
	        
	        //1.创建写出对象
	        XMLWriter writer=new XMLWriter(out,format);
	           
	        //2.写出Document对象
	        writer.write(doc);
	        
	        //3.关闭流
	        writer.close();
			System.out.println("修改完成");
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 修改菜单
	 * @param fileName
	 */
	public static void delMenu(String fileName){
		SAXReader reader = new SAXReader();
		Document doc = null;
		try {
			doc = reader.read(new File(fileName));
			Element root = doc.getRootElement();
			List<Element> eles   = root.elements("menu");
			for(Element el:eles){
				if(el.attributeValue("id").equals("02")){
					el.detach();
					System.out.println("id="+el.attributeValue("id")+"已删除");
				}
			}
			 //指定文件输出的位置
	        FileOutputStream out =new FileOutputStream(fileName);
	        
	        // 指定文本的写出的格式：
	        OutputFormat format=OutputFormat.createPrettyPrint();   //漂亮格式：有空格换行
	        format.setEncoding("UTF-8");
	        
	        //1.创建写出对象
	        XMLWriter writer=new XMLWriter(out,format);
	           
	        //2.写出Document对象
	        writer.write(doc);
	        
	        //3.关闭流
	        writer.close();
			System.out.println("删除完成");
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
